<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <!--    <a-form-item label='良品仓' :labelCol='{xs: { span: 3 },sm: { span: 5 }}' :wrapperCol='{xs: { span: 3 },sm: { span: 5 }}'>-->

    <div style="display: flex">
      <a-form layout="inline">
        <a-form-item label="交货单位">
          <j-dict-select-tag
            v-model:value="inputDepartment"
            placeholder="请输入交货单位"
            dict-code="sys_depart,depart_name,id"
            style="width: 150px"
            >交货单位
          </j-dict-select-tag>
        </a-form-item>
        <a-form-item label="良品仓">
          <j-dict-select-tag
            v-model:value="goodWarehouse"
            placeholder="请输入要收入的仓库"
            dict-code="hf_warehouse,warehouse_name,id"
            style="width: 150px"
            >良品
          </j-dict-select-tag>
        </a-form-item>
        <a-form-item label="不良品仓">
          <j-dict-select-tag
            v-model:value="noGoodWarehouse"
            placeholder="请输入要收入的仓库"
            dict-code="hf_warehouse,warehouse_name,id"
            style="width: 150px"
            >不良品
          </j-dict-select-tag>
        </a-form-item>
        <a-form-item label="报废仓">
          <j-dict-select-tag
            v-model:value="badWarehouse"
            placeholder="请输入要收入的仓库"
            dict-code="hf_warehouse,warehouse_name,id"
            style="width: 150px"
            >报废品
          </j-dict-select-tag>
        </a-form-item>
      </a-form>
      <a-button type="primary" style="margin-left: 100px" @click="handleOk">生成产品入库单</a-button>
    </div>

    <a-table
      ref="table"
      size="small"
      bordered
      rowKey="id"
      class="j-table-force-nowrap"
      :scroll="{ x: true }"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
      :customRow="clickThenSelect"
      @change="handleTableChange"
    >
      <template slot="planProduceNumber" slot-scope="text, record">
        {{ record.planProduceNumber }}
      </template>
      <template slot="good" slot-scope="text, record">
        <a-input-number v-model:value="record.good"></a-input-number>
      </template>
      <template slot="noGood" slot-scope="text, record">
        <a-input-number v-model:value="record.noGood"></a-input-number>
      </template>
      <template slot="bad" slot-scope="text, record">
        <a-input-number v-model:value="record.bad"></a-input-number>
      </template>
    </a-table>
  </j-modal>
</template>

<script>
import { getAction, postAction } from '@api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'QcCheckIn',
  mixins: [JeecgListMixin],
  props: {
    sourceId: {
      type: String,
      default: ''
    }
  },
  watch: {
    sourceId: {
      immediate: true,
      handler(val) {
        if (!this.mainId) {
          this.clearList()
        } else {
          this.queryParam['sourceId'] = val
          this.loadData1()
        }
      }
    }
  },
  data() {
    return {
      title: '品检入库',
      width: 1200,
      visible: false,
      queryParam: {},
      model: [],
      inputDepartment: 'eee4ae811d604c82b6a4a77bb0039368',
      goodWarehouse: '1777570854840557569',
      noGoodWarehouse: '1777570899157573633',
      badWarehouse: '1777570942979661825',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      /* 排序参数 */
      isorter: {
        column: 'createTime',
        order: 'desc'
      },
      /* table选中keys*/
      selectedRowKeys: [],
      /* table选中records*/
      selectionRows: [],
      /* 数据源 */
      dataSource: [],
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '30', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      /* table加载状态 */
      loading: false,
      columns: [
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode',
          scopedSlots: { customRender: 'materialCode' }
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '物料类型',
          align: 'center',
          dataIndex: 'materialType_dictText'
        },
        {
          title: '计划生产总数',
          align: 'center',
          dataIndex: 'planProduceNumber',
          scopedSlots: { customRender: 'planProduceNumber' }
        },
        {
          title: '良品',
          align: 'center',
          dataIndex: 'good',
          scopedSlots: { customRender: 'good' }
        },
        {
          title: '不良品',
          align: 'center',
          dataIndex: 'noGood',
          scopedSlots: { customRender: 'noGood' }
        },
        {
          title: '报废品',
          align: 'center',
          dataIndex: 'bad',
          scopedSlots: { customRender: 'bad' }
        }
      ],

      confirmLoading: false,
      validatorRules: {},
      url: {
        generateProductIn: '/production/hfProductmission/generateProductIn',
        list: '/production/hfProductmission/listHfProductmissionDetailByMainId'
      }
    }
  },
  methods: {
    clearList() {
      this.dataSource = []
      this.selectedRowKeys = []
      this.ipagination.current = 1
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      this.dataSource.forEach(item => {
        this.model.push({
          id: item.id,
          good: item.good,
          noGood: item.noGood,
          bad: item.bad,
          goodWarehouse: this.goodWarehouse,
          noGoodWarehouse: this.noGoodWarehouse,
          badWarehouse: this.badWarehouse,
          inputDepartment: this.inputDepartment
        })
      })

      if (this.inputDepartment === null) {
        this.$message.warning('请选择交货单位')
        this.model = []
        return
      }
      for (let i = 0; i < this.model.length; i++) {
        if (this.model[i].good === 0 && this.model[i].noGood === 0 && this.model[i].bad === 0) {
          this.$message.warning('请输入良品数量')
          this.model = []
          return
        }
      }
      this.confirmLoading = true

      postAction(this.url.generateProductIn, this.model)
        .then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
            this.close()
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.confirmLoading = false
          this.model = []
        })
    },
    handleCancel() {
      this.close()
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    loadData1(id) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      var params = this.getQueryParams() //查询条件
      params.sourceId = id
      this.loading = true
      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result
            this.dataSource.forEach((item, index) => {
              item.good = 0
              item.noGood = 0
              item.bad = 0
            })
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>

<style scoped></style>
